<?php
/**
 * Geneone: Content Management Framework
 * Search Page
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
require_once ('Gene/Search.php');

class Gene_Special_Search {
	/**
	 * Default Action
	 */
	function main() {
		$tpl =& Gene::getTemplate();
		$tpl->assign ("pagetitle", "Search");
		
		$tpl->append ("stylesheets", "search");
		
		$sources = Gene::getSetting("search_sources");
		$type = Gene_Request::getString("type", GENE_REQUEST_GET | GENE_REQUEST_POST);
		
		if (!$type OR !in_array($type, $sources)) {
			$type = Gene::getSetting("search_default_source");
		}
		
		$tpl->assign ("searchtype", $type);
		
		$source =& Gene_Search::getSource($type);
		
		if (PEAR::isError($source)) {
			$tpl->assign ("operror", $source->getMessage());
			$tpl->assign ("finalbody", $tpl->fetch("errors/operror"));
		} else {
			$query = Gene_Request::getString("q", GENE_REQUEST_GET | GENE_REQUEST_POST);
			$tpl->assign ("pagetitle", $query." - Search");

			if ($query) {
				$search =& $source->getSearch($query);
				if (PEAR::isError($search)) {
					$tpl->assign ("errormsg", $search->getMessage());
					$tpl->assign ("finalbody", $tpl->fetch("special/search"));
				} else {
					$tpl->assign ("search", true);
					$tpl->assign ("searchterm", $query);
					
					$page = Gene_Request::getInteger("page", GENE_REQUEST_GET | GENE_REQUEST_POST);
					if (!$page OR $page > $search->getNumPages()) {
						$page = 1;
					}
					$tpl->assign ("currentpage", $page);
					$tpl->assign ("currentpagestart", $page*10-9);
					if ($page*10 > $search->getNumResults()) {
						$tpl->assign ("currentpageend", $search->getNumResults());
					} else {
						$tpl->assign ("currentpageend", $page*10);
					}
					
					$tpl->assign ("numresults", $search->getNumResults());
					$tpl->assign ("numpages", $search->getNumPages());
					
					$tpl->assign ("results", $search->getPagedResults($page));
					
					$tpl->assign ("finalbody", $tpl->fetch("special/search"));
				}
			} else {
				$tpl->assign ("finalbody", $tpl->fetch("special/search"));
			}
		}
	}
}
 
?>